package com.snimay.app.entity;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.service.ServiceRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.google.gson.Gson;
import com.snimay.app.entity.repository.ItemsRepository;
import com.snimay.heibernate.ModelService;
import com.snimay.javacompiler.compiler.JavaFile;
import com.snimay.util.ToWeb;

/**   
 * @Title      : ItemsController.java
 * @Package    : com.snimay.app.entity
 * @Description: TODO 
 * @author     : xxy
 * @date       : 2018年5月26日 下午12:01:09
 * @version    : V1.0   
 */
@RestController
@RequestMapping("itemsclass")
@Scope("session")
public class itemsclassController {
	
	public  int i = 0;
	
	@SuppressWarnings("rawtypes")
	@Autowired
	ItemsRepository itemsRepository;
	
	@Autowired
	ModelService modelService;
	
	@Autowired
	JavaFile javaFile; 
	/**
	 * @Description: 用于获取数据.
	 * @author     : xxy
	 * @param name
	 * @return
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws
	 */
	@GetMapping("/itemsclass")
	public Object get() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		ToWeb toweb = new ToWeb();
	/*	Map<String, ClassMetadata> map = itemsRepository.getSessionFactory().getAllClassMetadata();
		List<Map> ls			=	new ArrayList<Map>();
		
		for(String s:map.keySet()) {
			//System.out.println(s);
			ClassMetadata clazz= map.get(s);
			Map	ms	=	(Map) modelService.getMode(clazz.getMappedClass());
			ls.add(ms);
		}
		toweb.setObjData(ls);*/
		return new Gson().toJson(toweb);
	}
	
	@GetMapping("/itemsclass1")
    public Object testSingle1() {
		ToWeb toweb = new ToWeb();
        ++i;
        System.out.println(itemsRepository);
        return new Gson().toJson(toweb);
    }

	@GetMapping("/itemsclass2")
    public Object testSingle2() {
		ToWeb toweb = new ToWeb();
        ++i;
        return new Gson().toJson(toweb);
    }
	
	

	
	@GetMapping("/itemsclass/get")
	public Object getx() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		ToWeb toweb = new ToWeb();
		//itemsRepository.getSessionFactory().
		/*Map<String, ClassMetadata> map = itemsRepository.getSessionFactory().getAllClassMetadata();
		List<Map> ls			=	new ArrayList<Map>();
		
		for(String s:map.keySet()) {
			//System.out.println(s);
			ClassMetadata clazz= map.get(s);
			Map	ms	=	(Map) modelService.getMode(clazz.getMappedClass());
			ls.add(ms);
		}
		toweb.setObjData(ls);*/
		
	//	itemsRepository.getSessionFactory().withOptions().
		//getConfiguration
		/*Configuration cf = new Configuration();
		cf.addAnnotatedClass(this.getClass()).buildSessionFactory();
		itemsRepository.getSessionFactory().getClassMetadata(entityClass)*/
		//itemsRepository.getSessionFactory().
		
		//StandardServiceRegistryBuilder cf	=	new StandardServiceRegistryBuilder().configure();
		
		Configuration cfg = new Configuration().configure();//.addAnnotatedClass(com.hibernate.demo.domain.News.class);  
		
		
		
		cfg.addAnnotatedClass(ClassLoader.getSystemClassLoader().loadClass("D:\\A.class"));
		
		//Configuration cf = new Configuration();
		  // 创建服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        
        
        // 创建会话工厂对象
        SessionFactory  sessionFactory  = cfg.buildSessionFactory(serviceRegistry);//new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
        
        
        
        
        Map<String, ClassMetadata> map = sessionFactory.getAllClassMetadata();
        List<Map> ls			=	new ArrayList<Map>();
        

		for(String s:map.keySet()) {
			//System.out.println(s);
			ClassMetadata clazz= map.get(s);
			Map	ms	=	(Map) modelService.getMode(clazz.getMappedClass());
			ls.add(ms);
		}
		toweb.setObjData(ls);
		
        // 会话对象
        Session session 				= sessionFactory.openSession();
        // 开启事物
        Transaction  transaction = session.beginTransaction();
        // 提交事物
        transaction.commit();
        // 关闭会话
        session.close();
        // 关闭会话工厂
        sessionFactory.close();
		
		return new Gson().toJson(toweb);
	}

 
}
